package ty.pub;

import java.io.Serializable;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

public class TransPacket implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 104852240502245447L;

	public TransPacket() {
		this.baseInfoMap = new LinkedHashMap<>();
		this.workStatusMap = new LinkedHashMap<>();
        this.calculationStatusMap = new LinkedHashMap<>();
    }
	
	public TransPacket(long timestamp, String deviceId, String ip, Map<String, String> baseInfoMap,
			Map<String, Map<Long, String>> workStatusMap) {
		super();
		this.timestamp = timestamp;
		this.deviceId = deviceId;
		this.ip = ip;
		this.baseInfoMap = baseInfoMap;
		this.workStatusMap = workStatusMap;
	}



	public String getDeviceId() {
		return deviceId;
	}

	public void setDeviceId(String deviceId) {
		this.deviceId = deviceId;
	}

	/**
	 * 时间戳
	 */
	private long timestamp;

	/**
	 * 工况数据的发送设备
	 */
	private String deviceId;

	/**
	 * 数据来源ip
	 */
	private String ip;

	/**
	 * 数据报文基础信息 key = 模板参数（TemplatePara）的参数编号parameterID value = 模板参数对应的数值
	 */
	private Map<String, String> baseInfoMap;

	/**
	 * 工况数据信息 key = 模板参数（TemplatePara）的参数编号parameterID value = 模板参数对应的数值
	 */
	private Map<String, Map<Long, String>> workStatusMap;

	/**
	 * 计算工况信息 key = 模板参数（TemplatePara）的参数编号parameterID value = 模板参数对应的数值
	 */
	private Map<String,Map<Long,String>> calculationStatusMap;


    public Map<String, Map<Long, String>> getCalculationStatusMap() {
        return calculationStatusMap;
    }

    public void setCalculationStatusMap(Map<String, Map<Long, String>> calculationStatusMap) {
        this.calculationStatusMap = calculationStatusMap;
    }

    public long getTimestamp() {
		return timestamp;
	}

	public void setTimestamp(long timestamp) {
		this.timestamp = timestamp;
	}

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public Map<String, String> getBaseInfoMap() {
		return baseInfoMap;
	}

	public void setBaseInfoMap(Map<String, String> baseInfoMap) {
		this.baseInfoMap = baseInfoMap;
	}

	public Map<String, Map<Long, String>> getWorkStatusMap() {
		return workStatusMap;
	}

	public void setWorkStatusMap(Map<String, Map<Long, String>> workStatusMap) {
		this.workStatusMap = workStatusMap;
	}
	
	public void addBaseInfo(String workStatus, String value) {
		baseInfoMap.put(workStatus, value);
	}

	public void addWorkStatus(String workStatus, long time, String value) {
		if (workStatusMap.containsKey(workStatus)) {
			workStatusMap.get(workStatus).put(time, value);
		} else {
			Map<Long, String> tmp = new LinkedHashMap<>();
			tmp.put(time, value);
			workStatusMap.put(workStatus, tmp);
		}
	}
	
	public Iterator<WorkStatusRecord> getWorkStatusMapIter(){
		return new NestedIterator();
	}
	
	public Iterator<Entry<String, String>> getBaseInfoMapIter(){
		return baseInfoMap.entrySet().iterator();
	}

	public class WorkStatusRecord {
		String workStatusId;
		long time;
		String value;

		public String getWorkStatusId() {
			return workStatusId;
		}

		public void setWorkStatusId(String workStatusId) {
			this.workStatusId = workStatusId;
		}

		public long getTime() {
			return time;
		}

		public void setTime(long time) {
			this.time = time;
		}

		public String getValue() {
			return value;
		}

		public void setValue(String value) {
			this.value = value;
		}

		public WorkStatusRecord(String workStatusId, long time, String value) {
			super();
			this.workStatusId = workStatusId;
			this.time = time;
			this.value = value;
		}

	}

	public class NestedIterator implements Iterator<WorkStatusRecord> {
		Iterator<Entry<String, Map<Long, String>>> outter = workStatusMap.entrySet().iterator();
		Entry<String, Map<Long, String>> current = null;
		Iterator<Entry<Long, String>> inner = null;

		@Override
		public boolean hasNext() {
			// TODO Auto-generated method stub
			return (outter.hasNext() || (inner != null && inner.hasNext()));
		}

		@Override
		public WorkStatusRecord next() {
			// TODO Auto-generated method stub
			if (outter.hasNext() && (current == null || !inner.hasNext())) {
				current = outter.next();
				inner = current.getValue().entrySet().iterator();
			}
			if (inner.hasNext()) {
				Entry<Long, String> tmp = inner.next();
				return new WorkStatusRecord(current.getKey(), tmp.getKey(), tmp.getValue());
			}
			return null;
		}
	}

}
